﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DTO;
using BUS;
using System.IO;
using System.Data.SqlClient;
using Uit_PhanQuyen;
using System.Reflection;
namespace GPBenhLanh
{
    public partial class frmHeThong : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public frmHeThong()
        {
            InitializeComponent();
            //getMainConnect();
            Uit.it_Conn.getConnectString();
            Uit.it_Conn.ConnectString =
                            axEnCrypt1.DeCrypt(Uit.it_Conn.ConnectString,
                                               Uit.it_Conn.getPasswordString());
            Uit.it_Conn.getConnect();

            getConnectPK();
        }
        private int[] arrDataTran = new int[2];
        public delegate void DlgSingOut(object sender, Object data);
        public event DlgSingOut SignOutEven;

        private static string strConnectPK = "";
        SqlConnection conpk;
        SqlConnection MainConn;

        private static string strPathHinh = "";
        /// <summary>
        /// get thong tin ket noi den database phKham
        /// </summary>
        public void getConnectStringPK()
        {
            string _sConnection = Uit.it_Conn.getConnectString("ConnectPhkham");
            strConnectPK = axEnCrypt1.DeCrypt(_sConnection, Uit.it_Conn.getPasswordString());
        }
        public SqlConnection getConnectPK()
        {
            getConnectStringPK();
            if (conpk == null || conpk.State == ConnectionState.Closed)
            {
                conpk = Uit.it_Conn.getConnect(strConnectPK);
            }
            return conpk;  
        }

        //public SqlConnection getMainConnect()
        //{          
        //    if (MainConn == null || MainConn.State == ConnectionState.Closed)
        //    {
        //        Uit.it_Conn.getConnectString();
        //        Uit.it_Conn.ConnectString =
        //                        axEnCrypt1.DeCrypt(Uit.it_Conn.ConnectString,
        //                                           Uit.it_Conn.getPasswordString());
        //       MainConn =  Uit.it_Conn.getConnect("ConnectString");
        //    }
        //    return MainConn;
        //}
        public void getPathStrHinh()
        {
            //string _sConnection = Uit.it_Conn.getConnectString("pathStrHinh");
            //strPathHinh = axEnCrypt1.DeCrypt(_sConnection, Uit.it_Conn.getPasswordString());
            strPathHinh = "";
            DataTable dtUrl =  GetThongTinSearchBUS.GetURLHinh(conpk);
            if (dtUrl != null)
            {
                strPathHinh = Convert.ToString(dtUrl.Rows[0]["GiaTri"]);
            }            
        }

        string _sThuMucLuu;
        public string sThuMucLuu
        {
            get { return _sThuMucLuu; }
            set { _sThuMucLuu = value; }
        }

        private string m_getVersion(string _Title)
        {
            string rs = "";
            string _name = Assembly.GetExecutingAssembly().GetName().Name + ".exe";
            string _strFile = Application.StartupPath + "\\" + _name;
            DateTime dFile = File.GetLastWriteTime(_strFile);

            rs = dFile.ToString("dd/MM/yyyy")
                + " - " + dFile.ToString("HH:mm");
            return rs;
        }

        private void frmHeThong_Load(object sender, EventArgs e)
        {
            LoadPhanQuyen frmPQ
             = new LoadPhanQuyen(ribbonMain, hensu.IDChuongTrinh, int.Parse(UserDTO.Id));
            getPathStrHinh();
            lblUser.Text = UserDTO.Name;
            lblVer.Text = m_getVersion("");
            timer1.Enabled = true;
            timer1.Start();
        }

        private void btnNhan_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmTiepNhan frm = new frmTiepNhan(1);
            frm.Text = "Tiếp nhận bệnh nhân";
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();            
        }

        private void btnDaNhan_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmDaTiepNhan frm = new frmDaTiepNhan(2);
            frm.Text = "Danh sách bệnh nhân đã xác nhận";
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();            
        }

        private void btnKQTeBao_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmKQTeBaoPAP frm = new frmKQTeBaoPAP();
            frm.Text = "Danh sách kết quả giải phẩu tế bào";
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();    
        }

        private void Thoat()
        {
            try
            {
                
                arrDataTran[0] = hensu.EXIT;
                arrDataTran[1] = hensu.EXIT;
                if (SignOutEven != null)
                {
                    SignOutEven(this, arrDataTran);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông báo");
            }
        }

        private void btnthoat_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
            Thoat();
        }

        private void frmHeThong_FormClosing(object sender, FormClosingEventArgs e)
        {

            Uit.it_Conn.disConnect();
            conpk.Close();
            conpk.Dispose();
            Uit.it_Conn.Conn.Dispose();
            timer1.Enabled = false;
            timer1.Stop();
            Thoat();
        }

        private void bardsdaxatnhan_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmThongKe frmTK = new frmThongKe();
            frmTK.Conpk = conpk;
            Uit_PhanQuyen.PhanQuyen.LoadForm(frmTK);
            frmTK.Show();
        }

        private void CreateDir()
        {
            string goc = strPathHinh;
            DateTime dt = DateTime.Today;
            string namthang = dt.Year.ToString() + Uit.it_String.Right("0" + dt.Month.ToString(), 2);
            if (!Directory.Exists(goc + "\\" + namthang.Substring(0, 4)))
            {
                Directory.CreateDirectory(goc + "\\" + namthang.Substring(0, 4));
            }
            if (!Directory.Exists(goc + "\\" + namthang.Substring(0, 4) + "\\" + namthang.Substring(2, 4)))
            {
                Directory.CreateDirectory(goc + "\\" + namthang.Substring(0, 4) + "\\" + namthang.Substring(2, 4));
            }
            this.sThuMucLuu = goc + "\\" + namthang.Substring(0, 4) + "\\" + namthang.Substring(2, 4);
        }

        private void btnChupHinh_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                CreateDir();
                frmChupHinh frmHinh = new frmChupHinh();
                frmHinh.sThuMucLuu = strPathHinh;
                Uit_PhanQuyen.PhanQuyen.LoadForm(frmHinh);
                frmHinh.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Thông Báo");
            }
        }

        private void btnKQRaDong_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmKetQuaGPB frmGPB = new frmKetQuaGPB();
            Uit_PhanQuyen.PhanQuyen.LoadForm(frmGPB);
            frmGPB.Show();
        }

        private void btnKqTeBaoFNA_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmKetQuaFNA frmFNA = new frmKetQuaFNA();
            Uit_PhanQuyen.PhanQuyen.LoadForm(frmFNA);
            frmFNA.Show();
        }

        private void btnTimKetQua_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmTimKQ frm = new frmTimKQ();
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();
        }

        private void barNhapTTCD_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmNhapTinCLS frm = new frmNhapTinCLS();
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();
        }

        private void barCDBoXung_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmChiDinhHM frm = new frmChiDinhHM();
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();
        }

        private void barHoanTien_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmHoanTien frm = new frmHoanTien();
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();

        }

        private void barThuLaoCatLanh_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmBaoCaoThuLaoCatLanh frm = new frmBaoCaoThuLaoCatLanh();
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();
        }

        private void barThuLaoGPB_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmBaoCaoThuLao_GPB frm = new frmBaoCaoThuLao_GPB();
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();
        }

        private void barBCThuLaoXN_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmBaoCaoThuLaoXN frm = new frmBaoCaoThuLaoXN();
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();
            
        }

        private void barDoiMatKhau_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmChangePass frm = new frmChangePass();
            frm.ConPK = conpk;
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();
        }

        private void btnDVGuiMau_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmDKDVGuiMau frm = new frmDKDVGuiMau();
            frm.conPK = conpk;
            frm.conGPB = MainConn;
            Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
            frm.Show();
        }

        private void btnChupHinh2_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                CreateDir();
                frmChupHinh_DS frmHinh = new frmChupHinh_DS();
                frmHinh.sThuMucLuu = strPathHinh;
                Uit_PhanQuyen.PhanQuyen.LoadForm(frmHinh);
                frmHinh.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Thông Báo");
            }
        }

        private void btnChupHinhNew_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                CreateDir();
                frmChupHinh_AF frmHinh = new frmChupHinh_AF();
                frmHinh.sThuMucLuu = strPathHinh;
                Uit_PhanQuyen.PhanQuyen.LoadForm(frmHinh);
                frmHinh.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Thông Báo");
            }
        }

        private void btnInDongL1Trang_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {                
                frmInHangLoat frm = new frmInHangLoat();
                Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Thông Báo");
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Text.Length == 0)
            {
                string s = hensu.FormTitle + " -##- Phiên bản ngày: " + m_getVersion("") ;
                Graphics graphics = this.CreateGraphics();

                while (true)
                {
                    SizeF size = graphics.MeasureString(" " + s, this.Font);
                    if ((int)size.Width > (this.Width - 50))
                        break;
                    else
                        s = " " + s;
                }

                this.Text = s;
            }
            else
            {
                this.Text = this.Text.Substring(1, this.Text.Length - 1);
            }
        }

        private void btnLichSu_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                frmLichSu frm = new frmLichSu();
                Uit_PhanQuyen.PhanQuyen.LoadForm(frm);
                frm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Thông Báo");
            }
        }

    }
}